﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Survey.Entity;

namespace Survey.DataAccessLayer.Data
{
    public class SurveyDbContext : DbContext
    {
        public DbSet<User> Users { get; set; }
        public DbSet<Questionnaire> Questionnaires { get; set; }
        public DbSet<Question> Questions { get; set; }
        public DbSet<QuestionOption> QuestionOptions { get; set; }
        public DbSet<QuestionFeedback> QuestionFeedback { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            // User模型配置
            modelBuilder.Entity<User>().Property(user => user.Email).HasMaxLength(64).IsRequired();
            modelBuilder.Entity<User>().HasIndex(user => user.Email).IsUnique();
            modelBuilder.Entity<User>().Property(user => user.PasswordHash).HasMaxLength(128).IsRequired();
            // Questionnaire模型配置
            modelBuilder.Entity<Questionnaire>().Property(questionnaire => questionnaire.Title).HasMaxLength(128);
            modelBuilder.Entity<Questionnaire>().Property(questionnaire => questionnaire.Describe).HasMaxLength(1024);
            modelBuilder.Entity<Questionnaire>().Property(questionnaire => questionnaire.CreatedAt).IsRowVersion();
            // Question模型配置
            modelBuilder.Entity<Question>().Property(question => question.Describe).HasMaxLength(1024).IsRequired();
            // QuestionOption模型配置
            modelBuilder.Entity<QuestionOption>().Property(option => option.Describe).HasMaxLength(1024).IsRequired();
        }

        public SurveyDbContext() : base()
        {
            Database.Connection.ConnectionString =
                ConfigurationManager.ConnectionStrings["DefaultConnectionString"].ConnectionString;
        }
    }
}
